package com.example.uiautomator;


import android.app.Instrumentation;
import android.content.Context;
import android.os.RemoteException;
import android.view.KeyEvent;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class MKTest {

    public Instrumentation mInstrumentation;
    public UiDevice mUiDevice;
    private Context mContext;

    @Before
    public void setUp() {
        mInstrumentation = InstrumentationRegistry.getInstrumentation();
        mContext = mInstrumentation.getContext();

        mUiDevice = UiDevice.getInstance(mInstrumentation);
    }



    @Test
    public void DemoTest() throws RemoteException {
        //查看手机最近所有任务
//        mUiDevice.pressRecentApps();

        //截屏，需要SD卡读取权限
//        mUiDevice.takeScreenshot(new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"11.png"));

        //isScreenOn():检查屏幕是否亮屏
        if (mUiDevice.isScreenOn()){
            //模拟按电源键，如果屏幕已经是关闭的则没有任何作用
            mUiDevice.sleep();
        }
        //自定义超时等待当前应用处于空闲状态
        mUiDevice.waitForIdle(2000);

        if (!mUiDevice.isScreenOn()){
            //模拟按电源键，如果屏幕是唤醒的没有任何作用
            mUiDevice.wakeUp();
        }
    }

    /**
     * 核心定位选择器
     * res:资源resource-id
     * text:内容
     * desc:描述信息 content-desc
     * checked:当前checked状态
     * focusable:当前focusable状态
     *
     * @throws RemoteException
     */
    @Test
    public void CoreLocationSelectorTest() throws RemoteException {

        //通过资源resource-id来查找按钮
//        mUiDevice.findObject(By.res("com.android.calculator2:id/digit7")).click();
        //通过查询text内容来查找
//        mUiDevice.findObject(By.text("8")).click();
        //通过描述信息 content-desc来查找
//        mUiDevice.findObject(By.desc("减")).click();

        //通过组合方式（resource-id和当前checked状态）来查找
//        mUiDevice.findObject(By.res("com.android.settings:id/switchWidget").checked(true)).click();

        //通过组合方式（resource-id和当前focusable状态）来查找
        mUiDevice.findObject(By.res("com.android.mms:id/embedded_text_editor")
                .focusable(true)).setText("自动输入的");
    }

    /**
     * 常见组建操作：
     * 点击：click(),
     * 长按：longClick(),
     * 拖动：drag(Point dest),
     * 输入：setText(String text)
     * <p>
     * <p>
     * 常见设备操作：
     * 点击：click(int x ,int y)
     * 按键：pressKeyCode(int keyCode)
     * 滑动：swipe(int startX,int startY,int endX,int endY,int steps) 其中1steps等于5ms
     */
    @Test
    public void calculatorTest() throws InterruptedException {
        //0.开机键
        mUiDevice.pressKeyCode(KeyEvent.KEYCODE_POWER);
        Thread.sleep(2000);
        //1. 初始化，滑动解锁-主屏幕   屏幕  滑动-按键
        mUiDevice.swipe(595, 1380, 519, 338, 10);
        Thread.sleep(3000);
        //2. 输入开机密码
        mUiDevice.findObject(By.res("com.android.keyguard:id/key2")).click();
        mUiDevice.findObject(By.res("com.android.keyguard:id/key4")).click();
        mUiDevice.findObject(By.res("com.android.keyguard:id/key6")).click();
        mUiDevice.findObject(By.res("com.android.keyguard:id/key8")).click();
        Thread.sleep(2000);
        mUiDevice.pressKeyCode(KeyEvent.KEYCODE_HOME);
        Thread.sleep(2000);
        //3. 打开计算器  计算器图标  坐标点击
        mUiDevice.click(137, 1069);
        Thread.sleep(2000);

        //4. 拖出科学计算器   科学计算器图标  拖动
        //找到抽屉的id即可
//        mUiDevice.findObject(By.res("com.android.calculator2:id/pad_advanced"))
//                .drag(new Point(224,1213),10);
//        Thread.sleep(1000);
        //5. 点击9  9按钮点击
        mUiDevice.findObject(By.res("com.android.calculator2:id/digit9")).click();
        //6. 按+  + 按键
        mUiDevice.findObject(By.res("com.android.calculator2:id/plus")).click();
        //7. 按键5 按钮 点击
        mUiDevice.findObject(By.res("com.android.calculator2:id/digit5")).click();
        //8. 点击= 按钮 点击
        mUiDevice.findObject(By.res("com.android.calculator2:id/equal")).click();
        Thread.sleep(5000);
        //9. 长按C清除  C按键  长按
        mUiDevice.findObject(By.res("com.android.calculator2:id/clear")).longClick();
        Thread.sleep(1000);
        //10. 输入5 按钮 点击
        mUiDevice.findObject(By.clazz("android.widget.EditText").focused(true)).setText("5");
    }


}
